/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
*                                                                        *
* Author: The ALICE Off-line Project.                                    *
* Contributors are mentioned in the code where appropriate.              *
*                                                                        *
* Permission to use, copy, modify and distribute this software and its   *
* documentation strictly for non-commercial purposes is hereby granted   *
* without fee, provided that the above copyright notice appears in all   *
* copies and that both the copyright notice and this permission notice   *
* appear in the supporting documentation. The authors make no claims     *
* about the suitability of this software for any purpose. It is          *
* provided "as is" without express or implied warranty.                  *
**************************************************************************/
//
//  Xianguo Lu <lu@physi.uni-heidelberg.de>
//

#include <TFile.h>
#include <TH2D.h>
#include <TTreeStream.h>

#include "AliTPCseed.h"

#include "AliESDEvent.h"
#include "AliESDfriend.h"

#include "AliCosmicTracker.h"
#include "AliESDCosmicTrack.h"
#include "AliTPCCosmicUtils.h"
#include "AliTRDdEdxUtils.h"
#include "AliTRDtrackV1.h"

#include "AliAnalysisTaskRecCosmic.h"

AliAnalysisTaskRecCosmic::AliAnalysisTaskRecCosmic(const TString chunk):
  AliAnalysisTaskSE("RecCosmic")
  , fESDEvent(0x0)
  , fESDfriend(0x0)
  , fChunk(chunk)
  , fhtrig(0x0)
  , fhnpair(0x0)
  , fhreuse(0x0)
  , fList(0x0)
{
  //
  //
  //

  fhtrig = new TH1I("a0_htrig", "", 40,0,40);
  fhnpair = new TH2I("a25_hnpair","",50, 0, 50, 100,0,100);
  fhreuse = new TH1I("a26_hreuse","",2,0,2);
  
  fList = new TList;
  fList->SetOwner();//------>>> Very important!!!!!!!
  
  fList->Add(fhtrig);
  fList->Add(fhnpair);
  fList->Add(fhreuse);
}

//____________________________________________________________
AliAnalysisTaskRecCosmic::~AliAnalysisTaskRecCosmic(){
  //
  // Destructor
  //
  delete fESDEvent;
  delete fESDfriend;

  delete fList;
}

void AliAnalysisTaskRecCosmic::Terminate(Option_t *) 
{
  //
  //do when Terminate
  //
  TFile *fh = new TFile(Form("TaskRecCosmicHist_%s.root", fChunk.Data()), "recreate");
  fList->Write();
  delete fh;
}

void AliAnalysisTaskRecCosmic::UserExec(Option_t *)
{
  //
  //Execute
  // 
  static Int_t ievt = 0;

  if(!AliTPCCosmicUtils::GetESD(fESDEvent, fESDfriend)){
    printf("xlulog no esd!!\n"); exit(1);
  }

  for(Int_t ii=0; ii<fESDEvent->GetNumberOfTracks(); ii++){
    const AliESDtrack * esdtrack = fESDEvent->GetTrack(ii);
    if(!esdtrack)
      continue;

    printf("test %d %d %f\n", ievt, ii, esdtrack->GetTRDsignal());
  }

  /*test ->

  //const Int_t btrig = AliTPCCosmicUtils::GetTrigger(fESDEvent)&(AliTPCCosmicUtils::k0HWU+AliTPCCosmicUtils::kTRDCO2);
  const Int_t btrig = AliTPCCosmicUtils::GetTrigger(fESDEvent);//&(AliTPCCosmicUtils::k0HWU+AliTPCCosmicUtils::kTRDCO2);
  fhtrig->Fill(btrig);
  if(!btrig){
    return;
  }

  const TClonesArray *stackCosmic = AliCosmicTracker::FindCosmic(fESDEvent, kFALSE); Int_t npair = stackCosmic->GetEntries();

  //const Int_t npair = fESDEvent->GetNumberOfCosmicTracks();

  fhnpair->Fill(fESDEvent->GetNumberOfTracks(), npair);
  if(!npair)
    return;

  printf("event %d ntrk %d npair %d\n", fESDEvent->GetEventNumberInFile(), fESDEvent->GetNumberOfTracks(), npair);

  for(Int_t ip=0; ip<npair; ip++){
    const AliESDCosmicTrack * esdcos = (AliESDCosmicTrack *) stackCosmic->At(ip);
    //const AliESDCosmicTrack * esdcos = fESDEvent->GetCosmicTrack(ip);

    printf("test: ievt %d, %d, %d %d %15f %15f\n", ievt, npair, esdcos->GetESDUpperTrackIndex(), esdcos->GetESDLowerTrackIndex(), esdcos->P(), esdcos->GetLowerPartner()->P());

    const Int_t kre = esdcos->IsReuse();
    fhreuse->Fill(kre);
  }

  delete stackCosmic;

  *///test<--

  ievt++;
}


